package com.ikun.yummy.activitys.login;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.InputType;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.ikun.yummy.App;
import com.ikun.yummy.R;
import com.ikun.yummy.activitys.sign.SignActivity;
import com.ikun.yummy.bean.UserInfo;
import com.ikun.yummy.manager.UserOpManager;
import com.ikun.yummy.utils.CommonUtils;
import com.ikun.yummy.utils.mail.SendMailUtil;
import com.ikun.yummy.utils.mail.SenderEmailInfo;

public class LoginActivity extends AppCompatActivity {
    private EditText username,password;
    private CheckBox cb;
    private UserOpManager userOpManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        userOpManager= new UserOpManager(this);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
    }

    private void initView() {
        username=findViewById(R.id.login_username);
        password=findViewById(R.id.login_password);
        cb=findViewById(R.id.login_cb);
    }

    public void register(View view) {
        startActivityForResult(new Intent(LoginActivity.this, SignActivity.class),1);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==2){//注册成功回来
            if (data!=null &&data.getBooleanExtra("signSuccess",false)){
                finish();
            }
        }
    }
    public void login(View view) {
        if (!username.getText().toString().equals("")&&!password.getText().toString().equals("")){
            if (cb.isChecked()){
                String type="username";
                if (CommonUtils.isEmail(username.getText().toString())){
                    type="email";
                }
                //可用账号、邮箱/若有需要可以在此调试
                //数据库有则登录成功
                UserInfo user =userOpManager.login(new UserInfo(username.getText().toString(), password.getText().toString(), "1", "",""),type);
                if (user!=null){
                    SharedPreferences.Editor edit = App.sp.edit();
                    edit.putString("username",user.username);
                    edit.putString("password",user.password);
                    edit.apply();
                    finish();
                }else{
                    Toast.makeText(this,"登录失败",Toast.LENGTH_SHORT).show();
                }
            }else {
                Toast.makeText(this,"请同意《协议》!",Toast.LENGTH_SHORT).show();
            }
        }else {
            Toast.makeText(this,"请输入完整",Toast.LENGTH_SHORT).show();
        }
    }
    public void findPassword(View view) {
        EditText editText=new EditText(this);
        editText.setHint("请输入邮箱");
        AlertDialog.Builder dialog=new AlertDialog.Builder(this);
        dialog.setTitle("提示").setView(editText).setPositiveButton("确定", (dialog1, which) -> {
            if (CommonUtils.isEmail(editText.getText().toString())){
                String yzm=CommonUtils.createRandom(4);
                SendMailUtil.send(new SenderEmailInfo("smtp.qq.com",
                        "25", "907319502@qq.com",
                        "vygsfvrybctwbcgg"),editText.getText().toString(),"Yummy验证码",yzm, null);
                EditText editText1=new EditText(this);
                editText1.setHint("请输入验证码");
                AlertDialog.Builder dialog2=new AlertDialog.Builder(this);
                dialog2.setTitle("提示").setView(editText1).setPositiveButton("确定", (d, w) -> {
                    if (yzm.equals(editText1.getText().toString())){
                        EditText pass=new EditText(this);
                        pass.setHint("请输入新密码");
                        pass.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
                        EditText pass2=new EditText(this);
                        pass2.setHint("请输再次输入");
                        pass2.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
                        LinearLayout v=new LinearLayout(this);
                        v.setOrientation(LinearLayout.VERTICAL);
                        v.addView(pass);
                        v.addView(pass2);
                        AlertDialog.Builder dialog4=new AlertDialog.Builder(this);
                        dialog4.setTitle("重置密码").setView(v).setPositiveButton("确定", (dialog3, which1) -> {
                            if (pass.getText().toString().equals(pass2.getText().toString())){
                                UserInfo user = userOpManager.queryUserByEmail(editText.getText().toString());
                                if (user==null){
                                    Toast.makeText(this, "改密失败，该邮箱不存在", Toast.LENGTH_SHORT).show();
                                    return;
                                }
                                userOpManager.updateByEmail(new UserInfo(user.username ,pass.getText().toString(),"1",null,editText.getText().toString()));
                                SharedPreferences.Editor edit = App.sp.edit();
                                edit.putString("username",user.username);
                                edit.putString("password",user.password);
                                edit.apply();
                                Toast.makeText(this,"已修改密码并登录",Toast.LENGTH_SHORT).show();
                                finish();
                            }else{
                                Toast.makeText(this,"两次密码不一样",Toast.LENGTH_SHORT).show();
                            }
                        }).setNeutralButton("",null).setCancelable(false).show();
                    }else {
                        Toast.makeText(this,"验证码错误",Toast.LENGTH_SHORT).show();
                    }
                }).setCancelable(false).setNeutralButton("取消",null).show();
            }else{
                Toast.makeText(this,"邮箱不合法",Toast.LENGTH_SHORT).show();
            }
        }).setNeutralButton("",null).show();
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (android.R.id.home==item.getItemId()){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}
